package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.IUserDao;
import com.dao.UserDaoImp;
import com.vo.Users;

public class LoginServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;utf-8");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String name = request.getParameter("userName");
		String pwd = request.getParameter("passWord");
		
		Users users = new Users();
		users.setEu_user_name(name);
		users.setEu_password(pwd);
		
		
		IUserDao dao = new UserDaoImp();
		//List<Users> list = null;
//		//鑾峰彇鎵�湁鐨勭敤鎴凤細
//		try {
//			//鎵�湁鐨刣ata宸茬粡鏀惧叆list涓�
//			list = dao.getAllData();
//			//灏嗘墍鏈塪ata甯﹀叆鍒版柊鐨勭晫闈�
//			
//		} catch (Exception e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//		request.setAttribute("alldata", list);
//		
		
		boolean flag = false;
		try {
			flag = dao.login(users);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag){
			List<Users> list = null;
			try {
				list = dao.getAllData();
//				for(int a = 0;a<list.size();a++){
//					users = list.get(a);
//					out.println(users.getEu_user_id()+"<br>"+users.getEu_user_name()+"<br>"+users.getEu_sex()+"<br>"+users.getEu_email()+"<br>"+users.getEu_mobile());
//				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("alldata", list);
			request.getRequestDispatcher("manage/user.jsp").forward(request, response);
			
		}else{
			out.print("<HTML>"+"<BR>");
			out.print("<HEAD><TITLE>很抱歉出现ERROR</TITLE></HEAD>"+"<BR>");
			out.print("<BODY>"+"<BR>");
			out.print("<TABLE BORDER=1>"+"<BR>");
			out.print("<TR>"+"<BR>");
			out.print("<TD>"+"<BR>");
			out.print("USERNAME PR PASSWORD ERROR");
			out.print("</TD>"+"<BR>");
			out.print("<TD>"+"<BR>");
			out.print("登录失败");
			out.print("</TD>"+"<BR>");
			out.print("</TR>"+"<BR>");
			out.print("</TABLE>"+"<BR>");
			out.print("</BODY>"+"<BR>");
			out.print("</HTML>"+"<BR>");
		}
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		doGet(request, response);
	}

}
